package com.atguigu.srb.core.controller;


import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.pojo.vo.UserBindVO;
import com.atguigu.srb.core.service.UserBindService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 前端控制器
 * </p>
 *
 * @author LiLi
 * @since 2021-09-23
 */
@RestController
@RequestMapping("/api/core/userBind")
@Api
public class UserBindController {

    @Autowired
    UserBindService userBindService;

    @ApiOperation("账户绑定")
    @PostMapping("notify")
    public String hfbNotify(HttpServletRequest request) {
        //解析汇付宝回调参数
        Map<String,Object> paramMap = RequestHelper.switchMap(request.getParameterMap());
        //调用业务层修改绑定状态
        userBindService.hfbNotify(paramMap);
        return "success";
    }
    @ApiOperation("账户绑定")
    @PostMapping("bind")
    public R bind(@RequestBody UserBindVO userBindVO, HttpServletRequest request){
        Long userId = 0l;
        String token = request.getHeader("token");
        if(!StringUtils.isEmpty(token)){
            userId = JwtUtils.getUserId(token);
        }
        //调用业务层，通过hfbapi接口生成汇付宝表单
        String formStr = userBindService.commitBindUser(userBindVO,userId);
        return R.ok().data("formStr",formStr);
    }
}

